
require "Object"
--Player class
Player = Object:new{
	updateID = nil,
	world = nil,
	destLocation = nil,
	speed = 50.0,
	life = 0,

	ccatFile = "player/player.ccat",
	bMoving = false,
}

function Player:walkAction(actName)
	--Misc:log("----------="..actName)
	return CCATFileCache:sharedCCATFileCache():findAction(actName, self.ccatFile);
end

function Player:onEnter()
	Misc:log("Player : onEnter .. " .. Misc:getNodeName(self))
	
	local function update(dt)
        self:tick(dt)
    end
	self.updateID = CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(update, 1/60.0, false)
end

function Player:onExit()
	Misc:log("88888888888888888888888888888888888="..self.updateID)
	CCDirector:sharedDirector():getScheduler():unscheduleScriptEntry(self.updateID)
end

function Player:create(playerFile, world, player)
	local newPlayer = player or Player:new()
	--self.__index = self
	newPlayer.world = world
	
	local cc2dNode = CC2DFileCache:sharedCC2DFileCache():addCC2DNode(playerFile)
	local ccPlayer = tolua.cast(cc2dNode:createCCNode(newPlayer.world.map), "CCSprite")
	Misc:setNodeName(ccPlayer, "player-man")
	
	tolua.setpeer(ccPlayer, newPlayer)
	
	local function onEnterOrExit(eventType)
		if eventType == kCCNodeOnExit then
			ccPlayer:onExit()
		else
			ccPlayer:onEnter()
		end
	end
	
	ccPlayer:registerScriptHandler(onEnterOrExit)
	
	if ccPlayer:isRunning() then
		ccPlayer:onEnter()
	end
	return ccPlayer
end

function Player:setLife(life)
	self.life = life
end

function Player:move(delta)
	local curLocation = self:getPositionLua()
	local newLocation = ccpAdd(curLocation, delta)
	
	local block = self.world.map:blockAt(newLocation)
	if block == 1 then
	elseif block == 2 then
		self:setOpacity(128)
		self:setPosition(newLocation.x, newLocation.y) 
	else
		self:setOpacity(255)
		self:setPosition(newLocation.x, newLocation.y) 
	end
end

function Player:tick(dt)
	local curLocation = self:getPositionLua()
	if self.destLocation and not ccpFuzzyEqual(self.destLocation, curLocation, 1) then
		local dir = ccpNormalize(ccpSub(self.destLocation, curLocation))
		local offset = ccpMult(dir, self.speed*dt)
		self:move(offset)
		bMoving = true
	else
		self:stopAllActions()
		bMoving = false
	end
end

function Player:moveTo(destLocation)
	local curLocation = self:getPositionLua()
	self.destLocation = destLocation
	
	local dir = ccpNormalize(ccpSub(self.destLocation, curLocation))
	
	self:stopAllActions()
	--Misc:log("xxxxxxxxxxxxx="..dir.y/dir.x)
	if dir.y>0 then
		if dir.x==0 or math.abs(dir.y/dir.x) > math.tan(math.pi/3.0) then
			self:runAction(self:walkAction("walk_back"))
		elseif math.abs(dir.y/dir.x) < math.tan(math.pi/6.0) then
			if dir.x > 0 then
				self:runAction(self:walkAction("walk_right"))
			else
				self:runAction(self:walkAction("walk_left"))
			end
		elseif dir.x > 0 and dir.y/dir.x < math.tan(math.pi/3.0) then
			self:runAction(self:walkAction("walk_right_back"))
		elseif dir.x < 0 and dir.y/dir.x > -math.tan(math.pi/3.0) then
			self:runAction(self:walkAction("walk_left_back"))
		end
	else
		if dir.x==0  or math.abs(dir.y/dir.x) > math.tan(math.pi/3.0) then
			self:runAction(self:walkAction("walk_front"))
		elseif math.abs(dir.y/dir.x) < math.tan(math.pi/6.0) then
			if dir.x > 0 then
				self:runAction(self:walkAction("walk_right"))
			else
				self:runAction(self:walkAction("walk_left"))
			end
		elseif dir.x > 0 and dir.y/dir.x > -math.tan(math.pi/3.0) then
			self:runAction(self:walkAction("walk_right_front"))
		elseif dir.x < 0 and dir.y/dir.x < math.tan(math.pi/3.0) then
			self:runAction(self:walkAction("walk_left_front"))
		end
	end
end